from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Boolean
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base

class ImageTask(Base):
    """图片任务表 - 图片包管理"""
    __tablename__ = "image_tasks"
    
    id = Column(Integer, primary_key=True, index=True, comment="任务ID")
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True, comment="用户ID")
    
    # 任务基本信息
    task_name = Column(String(200), nullable=False, comment="任务名称")
    description = Column(Text, comment="任务描述")
    
    # 任务状态
    is_active = Column(Boolean, default=True, comment="是否激活")
    
    # 统计信息
    image_count = Column(Integer, default=0, comment="图片数量")
    
    # 时间字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, comment="更新时间")
    
    # 关联关系
    user = relationship("User", back_populates="image_tasks")
    images = relationship("ImageTaskItem", back_populates="task", cascade="all, delete-orphan")

class ImageTaskItem(Base):
    """图片任务项表 - 存储具体的图片"""
    __tablename__ = "image_task_items"
    
    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey("image_tasks.id"), nullable=False, index=True, comment="任务ID")
    
    # 图片信息
    image_url = Column(Text, nullable=False, comment="图片URL")
    original_filename = Column(String(255), comment="原始文件名")
    
    # COS相关字段
    cos_url = Column(Text, comment="COS访问URL")
    cos_key = Column(String(500), comment="COS对象键")
    
    # 文件信息
    file_size = Column(Integer, comment="文件大小(字节)")
    file_type = Column(String(50), comment="文件类型")
    width = Column(Integer, comment="图片宽度")
    height = Column(Integer, comment="图片高度")
    
    # 上传状态
    upload_status = Column(String(50), default="pending", comment="上传状态: pending/uploading/success/failed")
    error_message = Column(Text, comment="错误信息")
    
    # 排序
    sort_order = Column(Integer, default=0, comment="排序顺序")
    
    # 时间字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, comment="更新时间")
    
    # 关联关系
    task = relationship("ImageTask", back_populates="images")